import {getContainerSources, getStorageSources, WORKSTATE, workSwitch} from "./public/CreepPublicAction.js";

/**
 * 升级者工作逻辑
 * @param {Creep} creep
 * @param sourcesIndex
 */
export const upgraderRun = (creep, sourcesIndex) => {
    //工作逻辑转换
    workSwitch(creep, RESOURCE_ENERGY)
    if (creep.memory[WORKSTATE]) {
        workCore(creep)
    } else {
        getSources(creep, sourcesIndex)
    }

};

const workCore = (creep) => {
    if (creep.upgradeController(creep.room.controller) === ERR_NOT_IN_RANGE) {
        creep.moveTo(creep.room.controller);
        // console.log(Memory.Rooms[creep.room.name].path.spawnToController)
        // creep.moveByPath(Room.deserializePath(Memory.Rooms[creep.room.name].path.spawnToController))
    }
}

const getSources = (creep, sourcesIndex) => {
    const link = Memory.Rooms[creep.room.name].links.targetLink.map((id) => Game.getObjectById(id))
    if (link.length > 0) {
        if (creep.withdraw(link[0], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
            creep.moveTo(link[0])
        }
    } else {

        // getStorageSources(creep, 1)
        // getContainerSources(creep,'#ba08fc',1000)
        if (!getStorageSources(creep, sourcesIndex, '#ba08fc')) {
            getContainerSources(creep, sourcesIndex, '#ba08fc', 1500, true)
        }
    }
}
